package io.renren.modules.canteens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.Result;
import io.renren.modules.canteens.dao.StockOrderDao;
import io.renren.modules.canteens.dao.StockOrdermxDao;
import io.renren.modules.canteens.dto.StockOrderDTO;
import io.renren.modules.canteens.dto.StockOrdermxDTO;
import io.renren.modules.canteens.entity.StockOrderEntity;
import io.renren.modules.canteens.service.StockOrderService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysDeptService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 食材采购表
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class StockOrderServiceImpl extends CrudServiceImpl<StockOrderDao, StockOrderEntity, StockOrderDTO> implements StockOrderService {
    @Autowired
    private StockOrderDao stockOrderDao;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;
    @Override
    public QueryWrapper<StockOrderEntity> getWrapper(Map<String, java.lang.Object> params) {
        String id = (String)params.get("id");

        QueryWrapper<StockOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 当前月的数据 食堂id
     * @param createDate
     * @param canteenId
     * @return
     */

    @Override
    public Map getOrderList(String createDate ,long canteenId,int limit,int page) {
        Map  map =new HashMap<>();
        if(StringUtils.isBlank(createDate)){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            createDate = sdf.format(date);
        }
        String  paging;
        if(0 ==limit){
            paging="";
        }else {
            paging="limit " + (page - 1) * limit + "," + limit;
        }
        createDate = " and  DATE_FORMAT(A.create_date,'%Y-%m')=DATE_FORMAT('"+ createDate +"','%Y-%m') ";
        List<StockOrderDTO> orderList = stockOrderDao.getOrderList(createDate,canteenId,paging);
        StockOrderDTO order = stockOrderDao.getOrdertotalMony(createDate, canteenId);
        StockOrderDTO totalPage = stockOrderDao.getTotalPage(createDate, canteenId);
         if(orderList.size()<1 || null ==order){
             Result result = new Result();
             if(null==createDate){
                 result.setMsg("当前月没有数据");
             }
             result.setCode(5001);
             result.setMsg(createDate+"没有数据");
             map.put("result",result);
             return map;
         }
        String name = sysDeptService.get(canteenId).getName();
        SysUserDTO sysUserDTO;
        for (StockOrderDTO orderDTO:orderList) {
            orderDTO.setCanteenName(name+"食材每日进货清单");
            sysUserDTO = sysUserService.get(orderDTO.getChedId());
            if(null !=sysUserDTO){
                orderDTO.setChedName(sysUserDTO.getRealName());
            }
            sysUserDTO = sysUserService.get(orderDTO.getCanteenAdminId());
            if(null !=sysUserDTO){
                orderDTO.setCanteenAdminName(sysUserDTO.getRealName());
            }
        }
        map.put("totalPage",totalPage.getTotalPage());
        map.put("orderList",orderList);
        map.put("totalPrice",order.getTotalPrice());
        return map;
    }

  /*  @Override
    public List<StockOrdermxDTO> getOrderMx(String createDate, long canteenId, int limit, int page) {
        String paging ="";
        if(StringUtils.isBlank(createDate)){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            createDate = sdf.format(date);
        }

        createDate = " and  DATE_FORMAT(A.create_date,'%Y-%m')=DATE_FORMAT('"+ createDate +"','%Y-%m') ";
        List<StockOrderDTO> orderList = stockOrderDao.getOrderList(createDate,canteenId,paging);
        if(orderList.size() < 1 ){
            return null;
        }
        List<StockOrdermxDTO> list = new ArrayList<>();
        for (StockOrderDTO orderDTO:orderList) {
            List<StockOrdermxDTO> stockOrdermxDTOList = stockOrdermxDao.getsupplierOrder(orderDTO.getStockOrderId());
            for (StockOrdermxDTO ordermxDTO:stockOrdermxDTOList) {
                List<StockOrdermxDTO> orderMx = stockOrdermxDao.getOrderMx(orderDTO.getStockOrderId(), ordermxDTO.getSupplierId());
                for (StockOrdermxDTO ordermxDTO1:orderMx) {
                    ordermxDTO1.setTotalPrice1(String.valueOf(ordermxDTO1.getTotalPrice()));
                    list.add(ordermxDTO1);
                }
                StockOrdermxDTO totalPrice = stockOrdermxDao.getTotalPrice(orderDTO.getStockOrderId(), ordermxDTO.getSupplierId());
                StockOrdermxDTO ordermxDTO1 = new StockOrdermxDTO();
                ordermxDTO1.setMealTime(orderDTO.getMealTime());
                ordermxDTO1.setTotalPrice1("合计 ： "+ totalPrice.getTotalPrice());
                list.add(ordermxDTO1);
            }
            StockOrdermxDTO totalPrice = stockOrdermxDao.getTotalMony(orderDTO.getStockOrderId());
            StockOrdermxDTO ordermxDTO = new StockOrdermxDTO();
            ordermxDTO.setMealTime(orderDTO.getMealTime());
            ordermxDTO.setTotalPrice1("共计 ： "+totalPrice.getTotalPrice());
            list.add(ordermxDTO);
        }
        String name = sysDeptService.get(canteenId).getName();
        for (StockOrdermxDTO ordermxDTO:list) {
            if(StringUtils.isNotBlank(ordermxDTO.getSupplierName())){
                BigDecimal unitPrice = new BigDecimal(ordermxDTO.getUnitPrice());
                BigDecimal rrivalNum = new BigDecimal(ordermxDTO.getArrivalNum());
                DecimalFormat df2 =new DecimalFormat("#.00");
                ordermxDTO.setTotalPrice1(df2.format(unitPrice.multiply(rrivalNum)));
                ordermxDTO.setCanteenName(name+"每日进货清单");
            }

        }
        return list;
    }
*/

}